﻿using System;
using System.Web.UI.WebControls;
using MedicalCare.DbLayer.Adaptors;
using MedicalCare.Utilities;
using StructureMap;
using MedicalCare.Messages;

namespace MedicalCare.Admin
{
    public partial class EquipmentsList : System.Web.UI.Page
    {
        private const string OrderKey = "CreateDT";
        private const string OrderValue = "DESC";
        private const int PageSize = 10;
        private string _whereString = "1=1";
        private IEquipmentsAdaptor _epAdaptor;
        //Champion 2013/1/3
        private int equipmentsId { get { return Convert.ToInt32(ViewState["equipmentTypeId"]); } set { ViewState["equipmentTypeId"] = value; } }
        private string action { get { return Convert.ToString(ViewState["action"]); } set { ViewState["action"] = value; } }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _epAdaptor = ObjectFactory.GetInstance<IEquipmentsAdaptor>();
                hideOrderby.Value = OrderKey; // 0514
                pager1.AlwaysShow = true;
                pager1.PageSize = PageSize;
                pager1.RecordCount = _epAdaptor.GetRecordsTotal(_whereString);
                BindDataList(true);
            }
        }

        private void BindDataList(bool rebindPager)
        {
            var currentPageSize = 1;
            try
            {
                if (Request.QueryString["Page"] != null)
                    currentPageSize = Convert.ToInt32(Request.QueryString["Page"]);
                else
                    currentPageSize = pager1.CurrentPageIndex;
            }
            catch
            {
                currentPageSize = 1;
            }

            var bpAdaptor = ObjectFactory.GetInstance<IEquipmentsAdaptor>();
            _whereString = "Valid!='N'";
            var source = bpAdaptor.GetPagingRecords(OrderKey, OrderValue, currentPageSize, PageSize, _whereString);

            var objPds = new PagedDataSource();
            objPds.DataSource = source;
            objPds.AllowPaging = true;
            objPds.PageSize = PageSize;
            EquipmentList.DataSource = objPds;
            EquipmentList.DataBind();
        }
        //Champion 2013/1/3
        private EquipmentsMessage GetEquipmentsMessage(string action)
        {
            EquipmentsMessage equipmentsMessage = null;
            if (ActionType.DELETE.ToString().Equals(action))
            {
                equipmentsMessage = new EquipmentsMessage()
                {
                    Id = equipmentsId,
                    ModifyUser = AppPage.CurrentUser.UserName,
                    Valid = "N"
                };
            }

            return equipmentsMessage;
        }

        protected void msgPager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            pager1.CurrentPageIndex = e.NewPageIndex;
            BindDataList(true);
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Server.Transfer("~/Admin/EquipmentDetail.aspx?action=" + ActionType.NEW.ToString());
        }
        // Champion 2013/1/3
        protected void EquipmentList_ItemCommand(object source, DataListCommandEventArgs e)
        {
            _epAdaptor = ObjectFactory.GetInstance<IEquipmentsAdaptor>();
            try
            {
                equipmentsId = Convert.ToInt32(e.CommandArgument);
                switch (e.CommandName.ToLower())
                {
                    case "del":
                        EquipmentsMessage equipmentsMessage = GetEquipmentsMessage(ActionType.DELETE.ToString());
                        int result = _epAdaptor.DeleteEquipmentsById(equipmentsMessage);
                        BindDataList(true);
                        break;
                    case "edit":
                        Server.Transfer("~/Admin/EquipmentDetail.aspx?action=" + ActionType.EDIT.ToString() + "&Id=" + equipmentsId.ToString());
                        break;
                }
            }
            catch
            {

            }
        }
    }
}